package cn.yunyichina.provider.clinic.service.impl;

import cn.yunyichina.provider.clinic.constant.MethodRouter;
import cn.yunyichina.provider.clinic.service.TianjiuService;
import cn.yunyichina.provider.clinic.vo.MoxibustonVoRequest;
import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.CacheService;
import cn.yunyichina.provider.framework.utils.UUIDUtil;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.base.OrgApp;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.StringUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 天灸
 * Created by Joe.zhou on 2017/6/19.
 */
@Service
public class TianjiuServiceImpl implements TianjiuService {

    private Logger logger = LoggerFactory.getLogger(TianjiuServiceImpl.class);

    /**
     * 生成天灸订单
     * @param data
     * @param sessionId
     * @interfaceName:yyt.clinic.tianjiu.order.add
     * @return
     */
    @Override
    public Response addTianjiuOrder(String data , String sessionId) {
        Response response = new Response();
        logger.info("addOrder, request data=" + data + ", sessionId=" + sessionId);
        MoxibustonVoRequest request = JsonUtils.parseObject(data, MoxibustonVoRequest.class);
        Map<String, String> params = new HashMap<>();

        try {
            Validator.getInstance().validate(request);
        } catch (ValidateException ex) {
            logger.info("addOrder 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            response.setResultCodeAndMessage("13", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }

        Org org = null;

        //根据hospitalCode查询医院信息
        Response orgResponse = BaseService.callHumpFromInternal(MethodRouter.GET_BASE_ORG_BY_CODE, JsonUtils.toJsonString(request.getHospitalCode()));
        if(orgResponse.getResult() != null){
            org = JsonUtils.parseObject(orgResponse.getResult(), Org.class);
        }else{
            response.setResultCodeAndMessage("1", "根据hospitalCode查询组织失败:" + request.getHospitalCode() + "！");
            return response;
        }

        OrgApp orgApp = null;

        Map<String, String> map = new HashMap<String, String>();
        map.put("org_app_code", request.getOrgAppCode());

        //根据appId查询应用信息
        Response orgAppResponse = BaseService.callHumpFromInternal(MethodRouter.GET_BASE_ORG_APP_BY_CODE, JsonUtils.toJsonString(map));
        if(orgAppResponse.getResult() != null){
            orgApp = JsonUtils.parseObject(orgAppResponse.getResult(), OrgApp.class);
        }else{
            response.setResultCodeAndMessage("1", "根据appId查询应用信息失败:" + request.getHospitalCode() + "！");
            return response;
        }


        if(org != null && orgApp != null){
            //订单生成
            OrderExtVo order = new OrderExtVo();
            order.setId(UUIDUtil.get32UUID());
            order.setOrderNo(CacheService.generateOrderNum(2, 5));
            order.setOrgId(org.getId());
            order.setOrgName(org.getOrgName());
            order.setOrgCode(org.getOrgCode());
            order.setPlatformId(orgApp.getOrgId());
            order.setPlatformCode(orgApp.getOrgCode());
            order.setPlatformName(orgApp.getOrgName());
            order.setPlatformOrgAppCode(orgApp.getOrgAppCode());
            order.setPlatformOrgAppName(orgApp.getOrgAppName());
            order.setOrderStatus((short) 1);
            order.setPayStatus((short) 1);
            order.setOrderType((short) 3);
            order.setUserName(request.getPatName());
            order.setPayMode((short) 13);
            order.setPayAmount(Integer.valueOf(request.getProjectAmount()));
            order.setTotalAmount(Integer.valueOf(request.getProjectAmount()));
            order.setPayCode("WXNATIVE");
            order.setIsInsurance((short) 0);
            order.setExtraParams(JsonUtils.toJsonString(request));

            response = BaseService.callHumpFromInternal("yyt.base.order.add" , JsonUtils.toJsonString(order));
            if("0".equals(response.getResultCode())){
                OrderExtVo orderExtVo = JsonUtils.parseObject(response.getResult(), OrderExtVo.class);
                if(orderExtVo == null){
                    response.setResultCodeAndMessage("1", "save order error!");
                    return response;
                }
            }else{
                response.setResultCodeAndMessage("1", "save order error!");
                return response;
            }
        }

        return response;
    }
}
